package com.seafwg.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seafwg.commonutils.R;
import com.seafwg.eduservice.entity.EduTeacher;
import com.seafwg.eduservice.entity.vo.TeacherQuery;
import com.seafwg.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author seafwg
 * @since 2020-12-20
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师表所有的数据：
     * @return
     */
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("/findAll")
//    public List<EduTeacher> list() {
//        return eduTeacherService.list(null);
//    }
    public R list() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    /**
     * 根据id逻辑删除讲师数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除讲师的信息")
    @DeleteMapping("/del/{id}")
//    public boolean removeById(@PathVariable String id) {
//        return eduTeacherService.removeById(id);
//    }
    public R removeById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        eduTeacherService.removeById(id);
        return R.ok();
    }

    /**
     * 分页条件查询：根据讲师名称name，讲师头衔level、讲师入驻时间gmt_create（时间段）查询
     * @param current 当前页码
     * @param limit 每页记录数
     * @return
     * @RequestBody：接收参数是必须使用Post提交
     */
    @ApiOperation(value = "多条件组合带分页查询")
    @PostMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(
         @ApiParam(name = "current", value = "当前页码", required = true)
         @PathVariable long current,
         @ApiParam(name = "limit", value = "每页记录数", required = true)
         @PathVariable long limit,
         @ApiParam(name = "queryTeacher", value = "查询对象", required = false)
         @RequestBody(required = false) TeacherQuery teacherQuery
    ) {
        //创建page对象：
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
//        //构建查询条件：
//        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
//        //多条件组合查询：
//        String name = teacherQuery.getName();
//        Integer level = teacherQuery.getLevel();
//        String begin = teacherQuery.getBegin();
//        String end = teacherQuery.getEnd();
//        if (!StringUtils.isEmpty(name)) {
//            wrapper.like("name", name);
//        }
//        // level为Integer:判断数据类型时判断不为空
//        if (level != null) {
//            wrapper.eq("level", level);
//        }
//        if (!StringUtils.isEmpty(begin)) {
//            wrapper.ge("gmt_create", begin);
//        }
//        if (!StringUtils.isEmpty(end)) {
//            wrapper.le("gmt_modified", end);
//        }
        //排序
//        wrapper.orderByDesc("gmt_create");

        //调用方法实现分页：
//        eduTeacherService.page(teacherPage, wrapper);


        eduTeacherService.pageQuery(teacherPage, teacherQuery);

        long total = teacherPage.getTotal();//总条数
        List<EduTeacher> records = teacherPage.getRecords();//当前记录数
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 新增讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    public R save(
       @ApiParam(name = "teacher", value = "讲师对象", required = true)
       @RequestBody EduTeacher eduTeacher
    ) {
        boolean flag = eduTeacherService.save(eduTeacher);
        return (flag = true)? R.ok() : R.error();
    }

    /**
     * 根据讲师id修改
     * @param id
     * @return
     */
    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("/find/{id}")
    public R getById(
        @ApiParam(name = "id", value = "讲师ID", required = true)
        @PathVariable String id
    ) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item", eduTeacher);
    }

    /**
     * 修改讲师内容：
     * @param eduTeacher:中的id是必填项
     * @return
     * Put提交也可以：要设置id
     */
    @ApiOperation(value = "讲师修改功能")
    @PostMapping("/update")
    public R updateById(
        @ApiParam(name = "teacher", value = "讲师对象", required = true)
        @RequestBody EduTeacher eduTeacher
    ) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        return (flag = true)? R.ok() : R.error();
    }
}

